package com.boxedinsoftware.betamax.dial;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

 public class OutgoingCallReceiverLegacy extends Activity {

	
	 
	 @Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			
			String full_number = this.getIntent().getDataString();
			String number = full_number.substring(full_number.indexOf(":") + 1, full_number.length());
			String initialNumber = number.replaceAll("%2B", "+");
			
			if(number.startsWith("%2B"))
			{
				
				
			   SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
			   String accessNumberString = preferences.getString("accnumber", "");
			   String provider = preferences.getString("betamaxProvider", "0");
			   String providerString = dialNow.PROVIDERS[new Integer(Integer.parseInt(provider))];
			   
			   if(accessNumberString.equalsIgnoreCase(""))
           	{
           		
           		Toast.makeText(getApplicationContext(), "Failed. No Access number provided.", Toast.LENGTH_LONG).show();
           		
           	}
           	else
           	{
           
           		number = number.replaceAll("%2B", "0");
           		number = "0" + number;
           		
           		number = number.replaceAll( "-", "" );
                number = number.replace(")", "");
                number = number.replace("(", "");
                number = number.replaceAll(" ", "");
                number = number.replaceAll("[a-z,A-Z]", "");
                if(number.substring(0, 1).equalsIgnoreCase("1"))
                {
               	 
               	 number = "+" + number;
               	 
                }
           	
           	   Uri uri = Uri.parse("tel:" + accessNumberString+"%2C" +number+"%23");
				
			   
			   Intent intent = new Intent(Intent.ACTION_CALL);
	      	   intent.setData(uri);
	      	   startActivity(intent);
	      	   
	      	   Toast.makeText(this, "Calling " + initialNumber + " via Betamax Provider : " + providerString, Toast.LENGTH_LONG).show();
			}
			}
			else
			{
				
				
				Toast.makeText(this, "Please use international format. Eg: +16476806419924", Toast.LENGTH_LONG).show();
				Intent intent = new Intent(OutgoingCallReceiverLegacy.this, dialNow.class);
				intent.setData(Uri.parse(initialNumber));
				startActivity(intent);
				
				
			}
			
			finish();
			
			
			
			
	 }

 }